$(function(){
    /*-------------将后台传过来的数据手动添加进推荐里面----------------*/
    
     $.ajax({
         type:"get",
         url:"index_nav",
         dataType : "json",
         success:function(data){
        	 
                var courselength = data.category.length;
                for(var i = 1; i <= courselength-1; i ++){
                    $("#course"+i).text(data.category[i-1].split("|")[0]);
                   // $("#course"+i).attr("catoryid",data.category[i-1].split("|")[1])
                    /*href="resources.html?Type_one=前端开发"、
                    */
                    $("#course"+i).parent().prop("href","resources.jsp?Type_one="+data.category[i-1].split("|")[1]);
                }
             
                
                function addLiDatas(data, obj){
                   var $appendHTml = "<ul>";

                   var len = data.length;
                  //  console.log(len);
                    
                   for(var i = 0; i < len-1; i ++){
                        $appendHTml += "<li class='ul-list' course-id='"+data[i].split("|")[1]+"' >" + data[i].split("|")[0] + "</li>";
                   }

                    console.log(len);
                   $appendHTml +=  "<li class='border-none ul-list' course-id='"+data[i].split("|")[1]+"' >" + data[len-1].split("|")[0] + "</li></ul>";
                    
                   obj.append($appendHTml);
                }
    


                addLiDatas(data["移动开发|3"], $("#func_bot3"));
                addLiDatas(data["数据库|4"], $("#func_bot4"));
                addLiDatas(data["前端开发|1"], $("#func_bot1"));
                addLiDatas(data["后端开发|2"], $("#func_bot2"));
                addLiDatas(data["云计算\u0026大数据|5"], $("#func_bot5"));
                addLiDatas(data["运维\u0026测试|6"], $("#func_bot6"));

         },
         error:function(){
             alert("请求数据出错1");
         }

    });
    
   
    /*点击进入二级页面*/
    $(".showfuc_box").on("click",".ul-list", function(){
        //alert($(this).text());
        window.location.href = "resources.jsp?Type_two="+$(this).attr("course-id");
    });
    
    
    /*滑屏到一定高度加载数据*/
    var scrollfalg = false;
    $(window).scroll(function(){
    	
    	var uid=$("#uid").val();
    	
        var  currentLocation=$(this).scrollTop();
        
		var $append = "";
		
        if(currentLocation>=200&&!scrollfalg){

        	if(uid == "" || uid == null){
        		console.log(uid+"----");
        	}else{
        		console.log(uid+"1111");
        		$.ajax({
        			 type:"get",
        	         url :"course_userCourse",
        	         dataType : "json",
        	         data:{
        	        	 "uid":uid
        	         },
        	         success:function(data){
        	        	     $append += "<div class='hot_recoment rec_rec'>"+
						            	"<div class='msg_card'>*个性推荐*</div>"+
						            	"<div class='show_recom'>"+
						            		"<div class='three_card'>"+
						            			"<ul class='rec_card'>";
        	        	     
        	        	     for(var j=0; j < 10; j++){
        	        	    	 var i = Math.floor(Math.random() * 4 + 1);
	        	        		 $append += "<li>"+
							                    "<a href='ResHome.jsp?courseId="+data.rec[j][3]+"'>"+
							                        "<div class='li_img'>"+
							                            "<img src='images/tuijian/personal/hot"+i+".png'>"+
							                            "<p>"+data.rec[j][1]+"</p>"+
							                        "</div>"+
							                        "<div class='classlevel'>课程级别·"+data.rec[j][2] +"</div>"+
							                        "<div class='createtime'><span>"+data.rec[j][0]+"</span>·人正在学习</div>"
							                    "</a>"+
							                  "</li>";
        	        	     }
	        	        	/* $.each(data.rec, function(index,value){
	                             var i = Math.floor(Math.random() * 4 + 1);
	        	        		 $append += "<li>"+
							                    "<a href='ResHome.jsp?courseId="+value[3]+"'>"+
							                        "<div class='li_img'>"+
							                            "<img src='images/tuijian/personal/hot"+i+".png'>"+
							                            "<p>"+value[1]+"</p>"+
							                        "</div>"+
							                        "<div class='classlevel'>课程级别·"+value[2] +"</div>"+
							                        "<div class='createtime'><span>"+value[0]+"</span>·人正在学习</div>"
							                    "</a>"+
							                  "</li>";
	        	        	 });*/

        					$append +="</ul></div></div></div>";
        					$(".hot_three").before($append);
        	         },
        	         error:function(){
        	        	 alert("请求失败！");
        	         }
        		})
				
        	}
            
        $.ajax({
         type:"get",
         url :"index_ordinary",
         dataType : "json",
         data:{},
         success:function(data){
                /*封装传值的函数*/
                function addRecoDatas(dat,obj,type){
                    obj.css("background","white");
                    $.each(dat, function(index, value){
                        var i = Math.floor(Math.random() * 4 + 1);
                        var $appendHTml = "<li>"+
                                            "<a href='ResHome.jsp?courseId="+value.courseId+"'>"+
                                                "<div class='li_img'>"+
                                                    "<img src='images/tuijian/"+type+"/"+type+i+".png'>"+
                                                    "<p>"+value.description+"</p>"+
                                                "</div>"+
                                                "<div class='classlevel'>课程级别·"+value.classLevel +"</div>"+
                                                "<div class='createtime'><span>"+value.studyNum+"</span>·人正在学习</div>"
                                            "</a>"+
                                          "</li>";

                        obj.append($appendHTml);
                    });
                }

                addRecoDatas(data.latest, $("#ul_move1"),"latest");
                addRecoDatas(data.hot, $("#ul_move2"),"hot");
                addRecoDatas(data.good, $("#ul_move3"),"good");
                //addRecoDatas(data.personal, $("#ul_move4"),"personal");

             },
            error:function(){
                 console.log("请求数据出错2");
             }

        });
            
            
            scrollfalg = true;
          
        }
    });
 
    
   
    
      
     //-----------推荐选项卡---------------------//
      
        var wide = $(".three_card").width();
        //alert(wide);
         $(".right_card").click(function(){
             
           
            var $ul = $(this).parent().find(".ppt_card");
             //
             var $li = $(this).parent().find(".ppt_card li:lt(5)");

             $(this).parent().find(".ppt_card").animate({"left":-wide*2 + "px"},"slow",function(){

                 //删掉之后位置不会发生改变，所以需要讲位置移动，给了时间为0
                $li.remove();
               $ul.append($li);
           }).animate({"left": -wide+"px"},0);
             
         });


          $(".left_card").click(function(){
            var $ul = $(this).parent().find(".ppt_card");
          
             var $li1 = $(this).parent().find(".ppt_card li:gt(9)");
              $(this).parent().find(".ppt_card").animate({"left": "0px"},"slow",function(){
                 $li1.remove();
                 $ul.prepend($li1);
              }).animate({"left":-wide+"px"},0);
              
              
        });
    
    /*--------------轮播左边的li---------------*/
    $(".panel1 li").mouseover(function(){
            clearInterval(time);
        $(this).find("a").css({
            "background-color":"black",
            "opacity": 0.4
        });
        $(this).find("div").css({
            "display":"block"
        });
    }).mouseleave(function(){
        $(this).find("a").css({
            "background-color":"transparent",
            "opacity": 1
        });
        $(this).find("div").css({
            "display":"none"
        });
    })
    
    
    
    /*---------------轮播下面的小功能图标-------------*/
        $(".move1").mouseover(function(){
            $(this).css({"top":"-15px"});
            $(this).css({"color":"red"});
        }).mouseleave(function(){
            $(this).css({"top":"0px"});
            $(this).css({"color":"black"});
        });
    
    
    /*----------------中间的轮播图---------------*/
        var time;
        var i = 0; //pic的位置
        var width = $(".pic").width();
        
        function setTime(){
            time = setInterval(function(){
                if(i < 5){
                    i ++;
                }else{
                    i = 0;
                }
                
                $(".panel2 .pic").eq(i).addClass("show").siblings(".pic").removeClass("show");
                $(".selects1 .cc").eq(i).addClass("h").siblings(".cc").removeClass("h");
            },6000);
        }
        
        setTime();
        
        $(".pic").mouseover(function(){
            clearInterval(time);
           
            $(".left").show();
            $(".right").show();
        }).mouseout(function(){
            setTime();
        
            $(".left").hide();
            $(".right").hide();
        });
    
        
        
        
        $(".left").click(function(){
        
           // alert(i);
            if(i > 0){
                i--;
            }
            else{
                i = 5;
            }
            
            $(".panel2 .pic").eq(i).addClass("show").siblings(".pic").removeClass("show");
            $(".selects1 .cc").eq(i).addClass("h").siblings(".cc").removeClass("h");
        }).mouseover(function(){
            $(this).show();
            $(".right").show();
        });
    
    
        $(".right").click(function(){
        
            if(i < 5){
                i++;
            }
            else{
                i = 0;
            }
            
            $(".panel2 .pic").eq(i).addClass("show").siblings(".pic").removeClass("show");
            $(".selects1 .cc").eq(i).addClass("h").siblings(".cc").removeClass("h");
        }).mouseover(function(){
            $(this).show();
            $(".left").show();
        });
        
        

        $(".cc").mouseover(function(){
            var index_ = $(this).index();
            i = index_;
            $(".panel2 .pic").eq(i).addClass("show").siblings(".pic").removeClass("show");
            $(".cc").eq(i).addClass("h").siblings(".cc").removeClass("h");
        });
});

    
